package com.pickgliss.ui.text
{
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * 渐变颜色文本
	 * @author Freeman
	 * 
	 */	
	public class GradientText extends Component
	{
		public static const P_alpha:String = "alpha";
		public static const P_color:String = "color";
		public static const P_frameFilters:String = "frameFilters";
		public static const P_ratio:String = "ratio";
		public static const P_textField:String = "textField";
		public static const P_size:String = "textSize";
		
		public function GradientText()
		{
		}

		protected var _filterString:String;
		protected var _frameFilter:Array;
		
		private var _colorStyle:String = "";
		private var _alphaStyle:String = "";
		private var _ratioStyle:String = "";
		
		
		private var _colors:Array;
		private var _alphas:Array;
		private var _ratios:Array;
		
		private var _gradientRotation:Number = 90;
		private var _currentFrame:int =  1;
		private var _currentMatrix:Matrix;
		private var _gradientBox:Shape;
		private var _textField:TextField;
		private var _textFieldStyle:String = "";
		private var _textSize:int;
		
		/**
		 * 修改颜色渐变的角度 
		 * @param value
		 * 
		 */		
		public function set gradientRotation(value:Number):void{
			_gradientRotation = value;
		}
		
		/**
		 * 颜色集合,可在xml内配置如 <b>colors="0xAF9234,0x4783DA|0xBAE727,0x75ABEA"</b>,这里每组两种颜色,一共两组
		 * @param color 颜色字符串
		 * 
		 */		
		public function set colors(color:String):void
		{
			if(color == _colorStyle)return;
			_colorStyle = color;
			
			_colors = [];
			var arr:Array  = _colorStyle.split("|");
			for (var i:int = 0; i < arr.length; i++)
			{
				_colors.push(arr[i].split(","));
			}
			onPropertiesChanged(P_color);
		}
		
		
		/**
		 * 透明度集合,可在xml内配置如 <b>alphas="0.5,0.8"</b>
		 * @param value 颜色字符串
		 * 
		 */		
		public function set alphas(value:String):void{
			if(value == _alphaStyle)return;
			_alphaStyle = value;
			if(_alphas)_alphas = [];
			_alphas = _alphaStyle.split(",");
			onPropertiesChanged(P_alpha);
		}
		
		/**
		 * 颜色比集合,可在xml内配置如 <b>ratios="0,255"</b>
		 * @param value 颜色比字符串
		 * 
		 */	
		public function set ratios(value:String):void{
			if(value == _ratioStyle)return;
			_ratioStyle = value;
			if(_ratios)_ratios = [];
			_ratios = _ratioStyle.split(",");
			onPropertiesChanged(P_ratio);
		}
		
		/**
		 * 
		 * @param value 设置每一帧的滤镜的字符串
		 * 具体看ComponentFactory.Instance.creatFilters
		 * 
		 */		
		public function set filterString(value:String):void
		{
			if(_filterString == value)return;
			_filterString = value;
			frameFilters = ComponentFactory.Instance.creatFrameFilters(_filterString);
		}
		
		/**
		 * 
		 * @param filter 设置每一帧的滤镜
		 * 
		 */		
		public function set frameFilters(filter:Array):void
		{
			if(_frameFilter == filter) return;
			_frameFilter = filter;
			onPropertiesChanged(P_frameFilters);
		}
		
		
		public function set text(value:String):void{
			_textField.text = value;
			refreshBox();
		}
		
		public function get text():String{
			return _textField.text;
		}
		
		public function set textSize(value:int):void
		{
			if(_textSize == value) return;
			_textSize = value;
			onPropertiesChanged(P_size);
		}
		
		public function get textSize():int
		{
			return _textSize;
		}
		
		public function get textField():TextField{
			return _textField;
		}
		
		public function set textField(tf:TextField):void
		{
			if(_textField == tf)return;
			_textField = tf;
			_textSize = int(_textField.defaultTextFormat.size);
			onPropertiesChanged(P_textField);
		}
		
		/**
		 * 文本框样式 
		 * @param stylename
		 * 
		 */		
		public function set textFieldStyle(stylename:String):void
		{
			if(stylename == _textFieldStyle)return;
			_textFieldStyle = stylename;
			textField = ComponentFactory.Instance.creat(_textFieldStyle);
		}
		
		override protected function addChildren():void{
			if(_textField){
				addChild(_textField);
				_textField.cacheAsBitmap = true;
			}
			
			if(_gradientBox){
				_gradientBox.x = _textField.x;
				_gradientBox.y = _textField.y;
				addChild(_gradientBox);
				_gradientBox.cacheAsBitmap = true;
				_gradientBox.mask = _textField;
			}
		}
		
		override public function get width():Number
		{
			return _textField.width;
		}
		
		public function get textWidth():Number
		{
			return _textField.textWidth;
		}
		
		public function getCharIndexAtPoint(x:Number, y:Number):int{
			return _textField.getCharIndexAtPoint(x,y);
		}
		
		/**
		 * 可以通过跳帧改变文本颜色 
		 * @param frameIndex
		 * 
		 */		
		public function setFrame(frameIndex:int):void
		{
			if(_currentFrame == frameIndex)return;
			_currentFrame = frameIndex;
			refreshBox();
			filters = _frameFilter[frameIndex-1];
		}
		
		override protected function onProppertiesUpdate():void{
			super.onProppertiesUpdate();
			if(_changedPropeties[P_textField] || _changedPropeties[P_color] || _changedPropeties[P_alpha] ||_changedPropeties[P_ratio] || _changedPropeties[P_size]){
				refreshBox();
			}
			if(_changedPropeties[P_frameFilters])
			{
				filters = _frameFilter[0];
			}
		}
		
		override public function dispose():void{
			
			if(_textField)ObjectUtils.disposeObject(_textField);
			_textField = null;
			_gradientBox.graphics.clear();
			_gradientBox = null;
			super.dispose();
		}
		
		private function refreshBox():void
		{
			var textFormat:TextFormat = _textField.getTextFormat();
			textFormat.size = _textSize;
			_textField.setTextFormat(textFormat);
			if(_textField.textWidth > _textField.width){
				_width = _textField.width = _textField.textWidth + 8;
			} 
			_currentMatrix = new Matrix();
			_currentMatrix.createGradientBox(_textField.width, _textField.height, Math.PI/2, 0, 0);

			if(_gradientBox == null)_gradientBox = new Shape();
			_gradientBox.graphics.clear();
			_gradientBox.graphics.beginGradientFill(GradientType.LINEAR, _colors[_currentFrame-1], _alphas, _ratios, _currentMatrix);
			_gradientBox.graphics.drawRect(0,0,_textField.width,_textField.height);
			_gradientBox.graphics.endFill();
		}
	}
}